<template>
  <div>

    <van-field label="手机号" v-model="mobile"></van-field>
    <van-button @click="sendsms" round :disabled="disabled">{{ mes }}</van-button>
    <van-field label="验证码" v-model="codes"></van-field>
    <van-button @click="login" round>提交</van-button>

  </div>
</template>

<script>
export default {
  name: "admin_login",
  data() {
    return {
      mes: '发送验证码',
      mobile: '',
      codes: '',
      time: 20,
      disabled: false
    }
  },
  methods: {
    send() {
      this.$axios.get('/admin/send?mobile=' + this.mobile)
        .then(res => {
          if (res.data.code == 200) {
            alert('发送成功')
            let s = setInterval(() => {
                this.time--
                this.disabled = true
                this.mes = this.time + '秒后再发'
                if (this.time == 0) {
                  clearInterval(s)
                  this.disabled = false
                  this.time = 20
                  this.mes = '发送验证码'

                }
              }, 1000
            )
          } else {
            alert(res.data.ts)

          }
        })

    },
    sendsms() {
      if (this.mobile == '' || this.mobile == null || this.mobile == undefined) {
        alert('手机号不能为空')
        return false
      }
      let re = /1[3-9]\d{9}$/

      if (!re.test(this.mobile)) {
        alert('手机号不符合')
        return false
      }
      this.send()

      // let flag = this.send()
      // alert(flag)
      // if (flag) {
      //   alert('发送成功')
      //   setInterval(this.mes = '倒计时', 6000)
      // } else {
      //   alert('发送失败，请重新发送')
      // }

    },
    login() {
      this.$axios.post('/admin/send', {'mobile': this.mobile, 'codes': this.codes}).then(res => {
        if (res.data.code == 200) {
          alert('登陆成功')
          localStorage.setItem('token',res.data.token)
        }
        else {
        alert(res.data.mes)
        }
      })

    }
  }
}
</script>

<style scoped>

</style>
